package com.word2voice;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.util.Date;

public class Application {

    public static void main(String[] args) {
        String text = "Hello Seven";
        audioFile(text);
    }
    public static void audioFile(String text) {
        try {
            //jacob.dll没成功安装，执行这一步会出错
            //构建音频格式 调用注册表应用
            Dispatch spAudioFormat = new ActiveXComponent("Sapi.SpAudioFormat").getObject();
            //音频文件输出流
            Dispatch spFileStream = new ActiveXComponent("Sapi.SpFileStream").getObject();
            //构建音频对象
            Dispatch spVoice = new ActiveXComponent("Sapi.SpVoice").getObject();

            //设置spAudioFormat音频流格式类型22
            Dispatch.put(spAudioFormat, "Type", new Variant(22));
            //设置spFileStream文件输出流的音频格式
            Dispatch.putRef(spFileStream, "Format", spAudioFormat);
            long time = new Date().getTime();
            //设置spFileStream文件输出流参数地址等
            Dispatch.call(spFileStream, "Open", new Variant("D:\\speech\\"+time+".mp3"), new Variant(3), new Variant(true));
            //设置spVoice声音对象的音频输出流为输出文件对象
            Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
            //设置spVoice声音对象的音量大小100
            Dispatch.put(spVoice, "Volume", new Variant(100));
            //设置spVoice声音对象的速度 0为正常速度，范围【..-2 -1 0 1 2..】
            Dispatch.put(spVoice, "Rate", new Variant(0));
            //设置spVoice声音对象中的文本内容
            Dispatch.call(spVoice, "Speak", new Variant(text));
            //关闭spFileStream输出文件
            Dispatch.call(spFileStream, "Close");

            //释放资源
            spVoice.safeRelease();
            spAudioFormat.safeRelease();
            spFileStream.safeRelease();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}
